package com.lovo.test.ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//取款界面
public class TakePanel extends JPanel implements ActionListener {

    private ATM frame;

    private JButton returnBut;//创建返回按钮对象
    private JButton confirmBut;//创建确认按钮对象
    private JTextField takeField;//金额输入文本框
    private JLabel remindLabel;//提示标签框

    public TakePanel(ATM frame) {
        this.frame = frame;
        //空布局
        this.setLayout(null);
        //设置透明
        this.setOpaque(false);

        //返回按钮,返回菜单界面
        returnBut = new JButton("返回");
        returnBut.setBounds(250, 280, 100, 50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //确认按钮,确认取款金额
        confirmBut = new JButton("确认");
        confirmBut.setBounds(450, 280, 100, 50);
        this.add(confirmBut);
        confirmBut.addActionListener(this);

        //以下是文本框
        takeField = new JTextField();//取钱文本框
        takeField.setBounds(350, 170, 100, 20);
        this.add(takeField);

        //以下是标签
        remindLabel = new JLabel("请输入取款金额：");
        remindLabel.setBounds(250, 170, 100, 20);
        add(remindLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == returnBut) {
            frame.switchPanel("菜单");//跳转到菜单界面

        } else if (e.getSource() == confirmBut) {
            takeMoney();

        }
    }

    //取款函数
    public void takeMoney() {
        int index = frame.getLoginPanel().getIndex();//获取账号对象下标
        int takeMoney = Integer.parseInt(takeField.getText());
        int Money = frame.getUsers().get(index).getMoney();//获取本金
        if (takeMoney>Money){
            JOptionPane.showMessageDialog(this,"账户余额不足");
        }else {
            Money-=takeMoney;
            frame.getUsers().get(index).setMoney(Money);//修改原集合内的本金
            takeField.setText("");
            JOptionPane.showMessageDialog(this,"取款成功");
            frame.preserve();
        }

    }
}
